﻿using Avalonia.Input;
using Avalonia.Platform.Storage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using dpa.Library.Helper;
using dpa.Library.Models;
using dpa.Library.Services;
using dpa.Models;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Ursa.Controls;

namespace dpa.Library.ViewModels;

public partial class FileDropViewModel(IExtractExcelService extractExcelService, IDocxService docxService) : ViewModelBase
{
    
    // 在FileDropViewModel中
    public ObservableCollection<FileItem> FileList { get; } = [];
    
    [ObservableProperty] 
    private FileItem _fileItem = new();

    [ObservableProperty]
    private bool _hasFiles;

    [RelayCommand]
    private void DropFile(DragEventArgs? e)
    {
        if (e is not null && e.Data.Contains(DataFormats.Files))
        {
            var files = e.Data.GetFiles();
            if (files != null)
            {
                foreach (IStorageItem storageItem in files)
                {
                    if (true)
                    {
                        FileList.Add(new FileItem
                        {
                            FileName = storageItem.Name,
                            FilePath = storageItem.Path.LocalPath,
                            FileExists = RegexHelper.IsOurFile(storageItem.Name)
                        });
                    }
                }
            }
        }
        
        HasFiles = FileList.Count > 0;
    }

    [RelayCommand]
    private async Task GenerateReport()
    {
        if (FileList.Count == 8)
        {
            AirportInfo extractAirportInfo = await extractExcelService.ExtractAirportInfo(FileList);

            (bool, string) generateReport = await docxService.GenerateReport(extractAirportInfo);

            if (generateReport.Item1)
            {
                MessageBoxResult messageBoxResult = await MessageBox.ShowAsync($"文件路径为{generateReport.Item2},==>是否要打开?","生成成功",MessageBoxIcon.Success,MessageBoxButton.OKCancel);
                if (messageBoxResult == MessageBoxResult.OK)
                {
                    try
                    {
                        // 跨平台兼容的打开方式
                        if (OperatingSystem.IsWindows())
                        {
                            Process.Start("explorer", $"\"{generateReport.Item2}\"");
                        }
                        else if (OperatingSystem.IsMacOS())
                        {
                            Process.Start("open", $"\"{generateReport.Item2}\"");
                        }
                        else if (OperatingSystem.IsLinux())
                        {
                            Process.Start("xdg-open", $"\"{generateReport.Item2}\"");
                        }
                    }
                    catch (Exception ex)
                    {
                        await MessageBox.ShowAsync(
                            $"无法打开文件: {ex.Message}",
                            "错误",
                            MessageBoxIcon.Error);
                    }
                }
            
            }
            else
            {
                await MessageBox.ShowAsync($"生成失败~~~~!{generateReport.Item2}", "错误", MessageBoxIcon.Error, MessageBoxButton.OK);
            }
        }
        else
        {
            await MessageBox.ShowAsync($"文件数量不完整", "错误", MessageBoxIcon.Error, MessageBoxButton.OK);
        }
    }

    [RelayCommand]
    private void RemoveFile(FileItem fileItem)
    {
        if (fileItem == null) return;
        
        FileList.Remove(fileItem);
        
        HasFiles = FileList.Count > 0;
    }
}